using UnityEngine;
using System;
using System.Collections;

public class TimeOnline : MonoBehaviour
{
    DateTime systemTime;//系统时间
    public static TimeOnline ins;
    DateTime myStartTime;//本地开始时间
    DateTime nowdt;//点前时间
    public int nowtime;//从本地运行到目前的总秒数
    void Awake()
    {
        ins = this;
        //systemTime= System.DateTime.Now;
        nowtime = -1;
        StartCoroutine(updateTime());
    }
    public IEnumerator updateTime()
    {
        while (true)
        {



            if (nowtime >= 0)
            {
                DateTime myEndTime = System.DateTime.Now;
                TimeSpan timeStay = myEndTime - myStartTime;

                nowtime = (int)(timeStay.Ticks/ 10000000);

                PlayerPrefs.SetInt(Defines.id + "_Time", PlayerPrefs.GetInt(Defines.id + "_Time") + nowtime);

                if (!PlayerPrefs.GetString(Defines.id + "_Day").Equals(GetNowTimeYYMMDD()))
                {
                    PlayerPrefs.SetString(Defines.id + "_Day", GetNowTimeYYMMDD());
                }
            }
            yield return new WaitForSeconds(0.5f);
        }
    }

    
    /// <summary>
    /// 判断是否可以领取在线奖励也在update 调用
    /// </summary>
    /// <returns></returns>
    public bool IsGetAward(int sectime)
    {
        int maxtime = sectime * 60;
        bool b = false;
        if (PlayerPrefs.GetInt(Defines.id + "_Time") > maxtime)
        {
            b = true;
        }
        return b;
    }

    /// <summary>
    /// 在线奖励时间 update调用
    /// </summary>
    /// <returns></returns>
    public string timeString(int sectime)
    {
        int maxtime = sectime * 60;
        string time = "00:00:00";
        if (!IsGetAward(sectime))
        {
            int CountTime = maxtime - PlayerPrefs.GetInt(Defines.id+"_Time");
            int hour = CountTime / 3600;
            int min = (CountTime % 3600) / 60;

            int sec = CountTime % 60;
            time = "";

            if (hour >= 10)
            {
                time += hour + ":";
            }
            else if (hour < 0)
            {
                time += "00:";
            }
            else
            {
                time += "0" + hour + ":";
            }
            if (min >= 10)
            {
                time += min + ":";
            }
            else if (min<0)
            {
                time += "00:";
            }
            else
            {
                time += "0" + min + ":";
            }
            if (sec >= 10)
            {
                time += sec;
            }
            else if (sec < 0)
            {
                time += "00";
            }
            else
            {
                time += "0" + sec;
            }
        }
        return time;
    }
   
    /// <summary>
    /// 保存系统时间
    /// </summary>
    /// <param name="time"></param>
    public void setTime(string time)
    {
        //Debug.Log("<color='#ff0000'>" + time + "</color>");
        //Debug.Log("<color='#ff0000'>" + time + "</color>");
        systemTime = Convert.ToDateTime(time);
        myStartTime= System.DateTime.Now; 
        nowtime = 0;
    }

    /// <summary>
    ///  获取当前时间
    /// </summary>
    /// <returns>hh：mm：ss</returns>
    public string getNowTimeHHMMSS()
    {
        //string time = "00:00:00";
        DateTime dtime = GetNowTime();
        return dtime.ToString("HH:mm:ss");
        //int sec = dtime.Second;
        //int hour = dtime.Hour;
        //int min = dtime.Minute;
      
        //if (hour >= 10)
        //{
        //    time += hour + ":";
        //}
        //else
        //{
        //    time += "0" + hour + ":";
        //}
        //if (min >= 10)
        //{
        //    time += min + ":";
        //}
        //else
        //{
        //    time += "0" + min + ":";
        //}
        //if (sec >= 10)
        //{
        //    time += sec;
        //}
        //else
        //{
        //    time += "0" + sec;
        //}
        //return time;
    }

    /// <summary>
    ///  获取当前时间 
    ///  </summary>
    /// <returns>返回YYMMDD</returns>
    public string GetNowTimeYYMMDD()
    {
        DateTime time = GetNowTime();
        return time.ToString("yyyy-MM-dd");
        //string month;
        //string day;
        //if (time.Month < 10)
        //{
        //    month = "0" + time.Month;
        //}
        //else
        //{
        //    month = time.Month.ToString();
        //}
        //if (time.Day < 10)
        //{
        //    day = "0" + time.Day;
        //}
        //else
        //{
        //    day = time.Day.ToString();
        //}
        //return (time.Year + "-" + month + "-" + day);
    }

    public string GetYearMouth()
    {
        DateTime time = GetNowTime();
        return time.ToString("yyyy-MM");

        //string month;
        //if (time.Month < 10)
        //{
        //    month = "0" + time.Month;
        //}
        //else
        //{
        //    month = time.Month.ToString();
        //}
        //return (time.Year + "-" + month);
    }
    /// <summary>
    /// 转换时间
    /// </summary>
    /// <param name="time"></param>
    public string getNowTimeYYMMDDHHMMSS(string _time)
    {
        if (_time.Length>1){
            DateTime time = Convert.ToDateTime(_time);

            return time.ToString("yyyy-MM-dd HH:mm:ss");
        }
        else
        {
            return "";
        }
      
        //string mon = "";
        //string day = "";
        //if (time.Month <= 10)
        //{
        //    mon = "0" + time.Month;
        //}
        //else
        //{
        //    mon = "" + time.Month;
        //}

        //if (time.Day <= 10)
        //{
        //    day = "0" + time.Day;
        //}
        //else
        //{
        //    day = "" + time.Day;
        //}

        //return (time.Year + "-" + mon + "-" + day + " " + time.Hour + ":" + time.Minute + ":" + time.Second);
    }


    /// <summary>
    /// 获取当前时间
    /// </summary>
    /// <returns>YYMMDDHHMMSS</returns>
    public string getNowTimeYYMMDDHHMMSS()
    {
        DateTime time = GetNowTime();
        return time.ToString("yyyy-MM-dd HH:mm:ss");
        //string mon = "";
        //string day = "";
        //if (time.Month<=10)
        //{
        //    mon = "0" + time.Month;
        //}
        //else
        //{
        //    mon = "" + time.Month;
        //}

        //if (time.Day <= 10)
        //{
        //    day = "0" + time.Day;
        //}
        //else
        //{
        //    day = "" + time.Day;
        //}

        //return (time.Year + "-" + mon + "-" + day+" "+ time.Hour+":"+time.Minute+":"+time.Second);
    }

    public string getAfterSecondsYYMMDDHHMMSS(double seconds)
    {
        DateTime time = GetNowTime().AddSeconds(seconds);
        return time.ToString("yyyy-MM-dd HH:mm:ss");
       // return (time.Year + "-" + time.Month + "-" + time.Day + " " + time.Hour + ":" + time.Minute + ":" + time.Second);
    }

    /// <summary>
    ///  获取当前时间
    /// </summary>
    /// <returns>Datetime</returns>
    public DateTime GetNowTime()
    {
        nowdt = systemTime.AddSeconds(nowtime);
        return nowdt;
    }

    public long getNowTime()
    {
        return (GetNowTime().ToUniversalTime().Ticks - 621355968000000000) / 10000;
    }

    /// <summary>
    /// 秒转换时分秒
    /// </summary>
    /// <param name="second"></param>
    /// <returns>HHMMSS</returns>
    public string GetShowTime(int second)
    {
        int min = (second % 3600) / 60;

        int sec = second % 60;
        string time = "00:";
        if (sec < 10)
        {
            time += "0" + min + ":0" + sec;
        }
        else
        {
            time += "0" + min + ":" + sec;
        }
        return time;
    }

    /// <summary>
    /// 分鐘转换时分
    /// </summary>
    /// <param name="min"></param>
    /// <returns>HHMMSS</returns>
    public string GetShowTimeHM(int min)
    {
        int h = min / 60;
        int m = min % 60;
        string time = "";
        if (m < 10)
        {
            time += h + ":0" + m;
        }
        else
        {
            time += h + ":" + m;
        }
        return time;
    }

    /// <summary>
    /// 秒转换时分
    /// </summary>
    /// <param name="min"></param>
    /// <returns>HHMMSS</returns>
    public string GetShowTimeHMS(int second)
    {
        int hour = second / 3600;
        int min = (second % 3600) / 60;
        int sec = second % 60;

        string dt = "";
        if (hour >= 10)
        {
            dt += hour + "";
        }
        else
        {
            dt += "0" + hour;
        }
        dt += ":";
        if (min >= 10)
        {
            dt += min + "";
        }
        else
        {
            dt += "0" + min;
        }
        dt += ":";
        if (sec >= 10)
        {
            dt += sec + "";
        }
        else
        {
            dt += "0" + sec;
        }
        return dt;
    }
    /// <summary>
    /// 获取年月日的int 时间
    /// </summary>
    /// <returns></returns>
    public int GetIntTime()
    {
        DateTime time = GetNowTime();
        //string m = "01";
        //string d = "01";
        //if (time.Month<10)
        //{
        //    m = "0" + time.Month;
        //}
        //if (time.Day < 10)
        //{
        //    d = "0" + time.Day;
        //}
        return int.Parse(time.ToString("yyyyMMdd"));
        //return int.Parse(time.Year + "" + m + "" + d);
    }

    /// <summary>
    /// 获取年月日的int 时间
    /// </summary>
    /// <returns></returns>
    public int GetNOWTime()
    {
        DateTime time = GetNowTime();
        return (int)((time.ToUniversalTime().Ticks - 621355968000000000) / 100000000);

        //time.TotalSeconds();
        //int sec = time.Second;
        //int hour = time.Hour;
        //int min = time.Minute;
        //int day = time.Day;
        //int mon = time.Month;
        //int 21 00 00 00 00
        //string dt = time.Day + "";

        //if (day>=10)
        //{
        //    dt += day + "";
        //}
        //else
        //{
        //    dt += "0" + day;
        //}

        //if (hour >= 10)
        //{
        //    dt += hour + "";
        //}
        //else
        //{
        //    dt += "0" + hour;
        //}
        //if (min >= 10)
        //{
        //    dt += min + "";
        //}
        //else
        //{
        //    dt += "0" + min;
        //}
        //if (sec >= 10)
        //{
        //    dt += sec + "";
        //}
        //else
        //{
        //    dt += "0" + sec;
        //}
        //return int.Parse(dt);
    }
    /// <summary>
    /// 获取时间差
    /// </summary>
    public int GetTimeDifference(string startTime)
    {
        DateTime start = Convert.ToDateTime(startTime);
        DateTime now = GetNowTime();
        // 求时间差
        TimeSpan ts = now - start;

       // Debug.LogError(ts+"+"+"+" + (int)ts.TotalSeconds);

        return (int)ts.TotalSeconds;
    }

    //时间差 小时差
    public int GetTimeHours(string startTime)
    {
        DateTime start = Convert.ToDateTime(startTime);
        DateTime now = GetNowTime();
        TimeSpan ts = now - start;
        return ts.Days * 24 + ts.Hours;
    }

    public DayOfWeek GetWeek()
    {
        DateTime time = GetNowTime();
        return time.DayOfWeek;
    }

    public string AddHour(int hour)
    {
        DateTime time = GetNowTime().AddHours(hour);
        return time.ToString("yyyy-MM-dd HH:mm:ss");
       // return (time.Year + "-" + time.Month + "-" + time.Day + " " + time.Hour + ":" + time.Minute);
    }
}
